#include "DarberFilter.h"
#include "ui_DarberFilter.h"

#include <QDir>
#include <QFileDialog>
#include <QTextStream>
#include <QTextBlock>
#include <QTextCharFormat>

DarberFilter::DarberFilter(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::DarberFilter)
{
    ui->setupUi(this);
}

DarberFilter::~DarberFilter()
{
    delete ui;
}

// static
static QString s_file_name;

void DarberFilter::on_BtnChooseFile_clicked()
{
    //选择单个文件
    QString curPath=QDir::currentPath();//获取系统当前目录
    //获取应用程序的路径
    QString dlgTitle="选择一个文件"; //对话框标题
    QString filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; //文件过滤器
    s_file_name = QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);

    ui->EditFilePath->setText(s_file_name);
}

using namespace std;
using std::string;

static QTextCharFormat set_text_color(QColor font,QColor background)
{
    QTextCharFormat format;

    format.setForeground(QBrush(font));
    //format.set

    return format;
}

// 支持8种颜色
static QTextCharFormat set_filter_color(int idx)
{
    QTextCharFormat format;

    const char *color_list[8] = {
        "red",
        "green",
        "purple",
        "orangered",
        "blue",
        "brown",
        "olive",
        "pink"
    };

    if(idx >= 8)
       format.setForeground(QBrush(QColor("lemonchiffon")));
    else
       format.setForeground(QBrush(QColor(color_list[idx])));

    return format;
}

void DarberFilter::on_BtnFilter_clicked()
{
    QString str;
    int lines;
    QString first_line;

    QTextCharFormat EditFileDefaultFormat = set_text_color(QColor("black"),QColor("white"));

    // 获取 EditFilters 的 每一行内容
    lines = ui->EditFilters->document()->lineCount();
    printf("filter lines = %d\n",lines);

    ui->EditFile->mergeCurrentCharFormat(EditFileDefaultFormat);

    first_line = ui->EditFilters->document()->findBlockByLineNumber(0).text();

    ui->LcdNumPercentage->setPalette(Qt::red);

    if (!s_file_name.isEmpty())
    {
        QFile aFile(s_file_name);  //以文件方式读出
        if (aFile.open(QIODevice::ReadWrite | QIODevice::Text))
        {
            QTextStream aStream(&aFile); //用文本流读取文件
            while (!aStream.atEnd())
            {
                QString filter_line;

                str = aStream.readLine().toStdString().c_str();
                //if(1 == lines)          // what the hell,一个空 plaintextedit居然，居然是1
                if( (1 == lines) && (0 == first_line.length() ) )
                {
                    ui->EditFile->appendPlainText(str);
                    continue;
                }

                for(int i = 0;i < lines;i++)
                {
                    double percent;

                    percent = i / lines;

                    ui->LcdNumPercentage->display(1.2);

                    filter_line = ui->EditFilters->document()->findBlockByLineNumber(i).text();
                    if(str.contains(filter_line,Qt::CaseSensitive))
                    {
                        ui->EditFile->mergeCurrentCharFormat(set_filter_color(i));
                        ui->EditFile->appendPlainText(str);
                        break;
                    }
                }
            }
        }
        aFile.close();
    }
}

void DarberFilter::on_BtnAntiFilter_clicked()
{
    QString str;
    int lines;
    QString first_line;

    QTextCharFormat EditFileDefaultFormat = set_text_color(QColor("black"),QColor("white"));

    lines = ui->EditRider->document()->lineCount();
    printf("filter lines = %d\n",lines);

    ui->EditFile->mergeCurrentCharFormat(EditFileDefaultFormat);

    first_line = ui->EditRider->document()->findBlockByLineNumber(0).text();

    if (!s_file_name.isEmpty())
    {
        QFile aFile(s_file_name);  //以文件方式读出
        if (aFile.open(QIODevice::ReadWrite | QIODevice::Text))
        {
            QTextStream aStream(&aFile); //用文本流读取文件
            while (!aStream.atEnd())
            {
                QString filter_line;

                str = aStream.readLine().toStdString().c_str();
                //if(1 == lines)          // what the hell,一个空 plaintextedit居然，居然是1
                if( (1 == lines) && (0 == first_line.length() ) )
                {
                    ui->EditFile->appendPlainText(str);
                    continue;
                }

                for(int i = 0;i < lines;i++)
                {
                    filter_line = ui->EditFilters->document()->findBlockByLineNumber(i).text();
                    if(str.contains(filter_line,Qt::CaseSensitive))
                        ;
                    else
                    {
                        ui->EditFile->mergeCurrentCharFormat(set_filter_color(i));
                        ui->EditFile->appendPlainText(str);
                        break;
                    }
                }
            }
        }
        aFile.close();
    }
}
